{extend name="../../base/view/common/base" /}
<!-- 主体 -->
{block name="body"}
<div class="think-box-shadow">
    <form class="layui-form gg-form-bar border-t border-x" onsubmit="return false">

<!--        <div class="layui-input-inline" style="width:300px;">-->
<!--            <select name="punterId" id="" lay-search>-->
<!--                <option value="">客户名称</option>-->
<!--                {volist name='punterList' id='pl'}-->
<!--                <option value="{$pl.id}" {if }>{$pl.name}</option>-->
<!--                {/volist}-->
<!--            </select>-->
<!--        </div>-->

        <div class="layui-input-inline" style="width:300px;">
            <input type="text" name="orderNum" placeholder="合同单号"  class="layui-input" autocomplete="off" />
        </div>
        <div class="layui-input-inline" style="width:300px;">
            <input type="text" name="xsDw" placeholder="销售单位"  class="layui-input" autocomplete="off" />
        </div>
        <div class="layui-input-inline" style="width:150px;">
            <button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="webform"><i class="layui-icon layui-icon-search mr-1"></i>搜索</button>
            <button type="reset" class="layui-btn layui-btn-reset" lay-filter="reset">清空</button>
        </div>
    </form>
    <table class="layui-table margin-top-10" lay-filter="mxTable" lay-skin="line" id="mxTable">
    </table>
    <script type="text/html" id="toolbarDemo">
        {if isset($info.status)&&$info.status==2}
        <button class="layui-btn layui-btn-sm layui-btn-normal " lay-event="subt" type="button">确认</button>
        {/if}
    </script>
</div>
{/block}
{block name="script"}
<!-- /主体 -->
<script>
    const moduleInit = ['tool'];
    function gouguInit() {
        var form =layui.form;

        var treeTable = layui.table;
        // $.extend(treeTable, {config: {checkName: 'checked'}});
        var tableIns = treeTable.render({
            elem: '#mxTable'
            , url: "{:url('api/getContractList')}?type=1&isOver=1&punterId={$purveyorId}"
            , method: 'post'
            , toolbar: '#toolbarDemo',
            defaultToolbar: []
            , cellMinWidth: 80
            , cols: [[
                {type: 'checkbox', fixed: 'left'},
                {fixed: 'left', field: 'id', title: 'ID', unresize: true, hide: true}
                , {field: 'dName', title: '销售单位'}
                , {field: 'orderNum', title: '合同号'}
                , {field: 'countMoney', title: '合同金额'}
                , {field: 'receiptedMoney', title: '已到款金额'}
                , {field: 'outMoney', title: '已出库金额'}
                , {field: 'kpMoney', title: '已开票金额'}
                , {field: 'unPay', title: '未收金额'}
                , {field: 'money', title: '本次到款金额',edit:"number"}
            ]],
            parseData:function (data) {
                console.log('pppppId',"{$purveyorId}")
                let tableData=[];
                data.data.forEach(function (item) {
                    item.money=item.unPay;
                    tableData.push(item)
                })
                data.data=tableData;
                return data;
            }
            , page: true
        });
        form.on('submit(webform)', function (data) {
            tableIns.reload({
                where: data.field,
                page: {
                    curr: 1
                }
            });
            return false;
        });
        treeTable.on('toolbar(mxTable)', function (obj) {
            var checkStatus = treeTable.checkStatus(obj.config.id);
            console.log(checkStatus)
            switch (obj.event) {
                case 'add':
                    break;
                case 'subt':
                    var data = checkStatus.data;
                    var param = []
                    let islert=0;
                    let isnullMoney=0;
                    console.log('dddd',data)
                    data.forEach(function (value, index) {
                        if (parseFloat(value.unPay)<parseFloat(value.money)){
                            islert=1;
                        }
                        if (value.money==''||parseFloat(value.money)<=0){
                            isnullMoney=1;
                        }
                        if (value.money!=''&&parseFloat(value.money)>0){
                            param.push(value)
                        }
                    })
                    if (islert!=0){
                        layer.msg('到款金额大于未付金额');
                        return;
                    }
                    if (isnullMoney>0){
                        layer.msg('请填写金额');
                        return;
                    }
                    httpRequest("{:url('')}?receiptId={$receiptId}",{data:param},function (res) {
                        if (res.code==0){
                            setTimeout(function () {
                                parent.parent.location.reload();
                            },1000)

                        }
                    })

                    break;
                case 'import':

                    break;

            }
            ;
        })

    }

    function saveMx(data) {
        parent.saveAddMx(data)

    }
</script>
<!-- 脚本 -->
{/block}


